.\"
.\" Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 2021 Oxide Computer Company
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.Dd November 29, 2021
.Dt KSTAT_CHAIN_UPDATE 3KSTAT
.Os
.Sh NAME
.Nm kstat_chain_update
.Nd update the kstat header chain
.Sh Library
.Lb libkstat
.Sh SYNOPSIS
.In kstat.h
.Ft kid_t
.Fo kstat_chain_update
.Fa "kstat_ctl_t *kc"
.Fc
.Sh DESCRIPTION
The
.Fn kstat_chain_update
function brings the user's kstat header chain in sync with that of the kernel.
The kstat chain is a linked list of kstat headers
.Po
.Vt kstat_t Ns 's
.Pc
pointed to by
.Ql kc->kc_chain ,
which is initialized
by
.Xr kstat_open 3KSTAT .
This chain constitutes a list of all kstats currently in the system.
.Pp
During normal operation, the kernel creates new kstats and deletes old ones as
various device instances are added and removed, thereby causing the user's copy
of the kstat chain to become out of date.
The
.Fn kstat_chain_update
function detects this condition by comparing the kernel's current kstat chain
ID
.Pq KCID ,
which is incremented every time the kstat chain changes, to the user's KCID,
.Ql kc->kc_chain_id .
If the KCIDs match,
.Fn kstat_chain_update
does nothing.
Otherwise, it deletes any invalid kstat headers from the user's kstat chain,
adds any new ones, and sets
.Ql kc->kc_chain_id
to the new KCID.
All other kstat headers in the user's kstat chain are unmodified.
.Pp
Because of the deletion process, any
.Vt kstat_t
structures retrieved by the
.Xr kstat_lookup 3KSTAT
function or data pointers obtained through the
.Xr kstat_data_lookup 3KSTAT
function are invalidated by a call to
.Fn kstat_chain_update .
New lookup calls must be made to obtain fresh structures after each chain
update.
.Sh RETURN VALUES
Upon successful completion,
.Fn kstat_chain_update
returns the new KCID if the kstat chain has changed and 0 if it has not changed.
Otherwise, it returns
.Sy -1
and sets
.Va errno
to indicate the error.
.Sh FILES
.Bl -tag -width Pa
.It Pa /dev/kstat
kernel statistics driver character device
.El
.Sh ERRORS
The
.Fn kstat_chain_update
function will fail if:
.Bl -tag -width Er
.It Er EAGAIN
The kstat was temporarily unavailable for reading or writing.
.It Er ENOMEM
Insufficient storage space is available.
.It Er ENXIO
The given kstat could not be located for reading.
.It Er EOVERFLOW
The data for the given kstat was too large to be stored in the structure.
.El
.Sh INTERFACE STABILITY
.Sy Committed
.Sh MT-LEVEL
.Sy Unsafe
.Sh SEE ALSO
.Xr kstat 3KSTAT ,
.Xr kstat_lookup 3KSTAT ,
.Xr kstat_open 3KSTAT ,
.Xr kstat_read 3KSTAT ,
.Xr attributes 7
